import java.util.Deque;
import java.util.LinkedList;

public class num662 {
    public int widthOfBinaryTree(TreeNode root) {
        Deque<amnode> queue = new LinkedList<>();
        queue.offer(new amnode(root,0,0));
        int curdeth = 0,left = 0,ans = 0;
        while (!queue.isEmpty()){
            amnode a = queue.poll();
            if(a.node != null) {
                queue.offer(new amnode(a.node.left, a.d + 1, a.p * 2));
                queue.offer(new amnode(a.node.right, a.d + 1, a.p * 2 + 1));
                if (curdeth != a.d) {
                    curdeth = a.d;
                    left = a.p;
                }
                ans = Math.max(ans, a.p - left + 1);
            }
        }
        return ans;
    }
}
class amnode{
    TreeNode node;
    int d;
    int p;
    public amnode(TreeNode node,int d,int p){
        this.node = node;
        this.p = p;
        this.d = d;
    }
}
